/**
 * login.html page script
 * @author czp
 * date 2018-05-01
 */
var temp = 0;
isInitSys();
$('#login').on("click", function() {
    var user = $('input[name="user"]').val();
    var pwd = $('input[name="pwd"]').val();
    if (user.trim() === "") {
        $('#msg').text("用户名不能为空！");
    } else if (pwd.trim() === "") {
        $('#msg').text("密码不能为空！");
    } else {
        loginCheck(user, pwd);
    }
});

$('#hideLogoutBtn').on("click", function () {
    temp ++;
    if (temp === 10) {
        temp = 0;
        $.ajax({
            url : contextPath + '/login/loginOut',
            data : { "state" : "COERCE_LOGOUT" },
            type : 'POST'
        });
    }
});

function loginCheck(user, pwd) {
    var password = pwd;
    $.ajax({
        url : contextPath + '/login/loginStatus',
        type : 'POST',
        data : {"user" : user, "pwd" : password},
        success : function (data) {
            if (data === "INIT") {
                window.location.href = contextPath + "/login/initWizard";
            } else if (data === "SUCCESSFUL") {
                window.location.href = contextPath + "/ref/index";
            } else if (data === "INTERCEPTOR") {
                $('#msg').text("不能重复登陆！");
            } else {
                $('#msg').text("用户名或密码输入错误！");
            }
        },
        error : function () {
            $('#msg').text("请检查数据库连接！");
        }
    });
}

function isInitSys() {
    $.ajax({
        url : contextPath + '/login/isInitSys',
        type : 'POST',
        success : function (data) {
            if (data) $("#lostPwd").css("display","block");
            else $("#lostPwd").css("display","none");
        },
        error : function () {
            $('#msg').text("请检查数据库连接！");
        }
    });
}
